package org.example.demo7;

import java.sql.*;

public class DBTest {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/library_management?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String username = "root"; // 你的数据库用户名
        String password = "232930"; // 你的数据库密码
        String sql="select * from user where username=? and password=?";
        String name="admin";
        String pass="admin123";
        int id=0;
        String email=null;
        Connection conn=null;
        PreparedStatement prep=null;
        ResultSet rs=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
             conn = DriverManager.getConnection(url, username, password);
             prep = conn.prepareStatement(sql);
            prep.setString(1,name);
            prep.setString(2,pass);
            rs= prep.executeQuery();
            while(rs.next()){
                id = rs.getInt(1);
                email = rs.getString(6);
            }

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        catch(SQLException e){  // 修复：添加异常参数e
            throw new RuntimeException(e);
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                prep.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println(id);
        System.out.println(email);
    }
}
